package nio.one;

import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/*
    测试读取文件
 */
public class ChannelTest02 {
    public static void main(String[] args) {
        try {
            //获取文件通道
            FileInputStream fis = new FileInputStream("E:\\mycode\\io\\src\\nio\\one/test.txt");
            FileChannel fileChannel = fis.getChannel();
            //创建缓冲区
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            //读取文件中的数据到缓冲区
            fileChannel.read(buffer);
            //获取缓冲区字节数组
            buffer.flip();
            System.out.println(new String(buffer.array(),0,buffer.remaining()));
            fileChannel.close();
            fis.close();
        }catch (Exception e){

        }
    }
}
